package interlocking.controller;

import interlocking.core.InterlockingCore;
import interlocking.model.*;
import interlocking.ui.MainWindow;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class InterlockingController {
    private InterlockingCore core;
    private MainWindow mainWindow;


    public InterlockingController() {
        this.core = new InterlockingCore();
        this.mainWindow = new MainWindow(this);
        this.mainWindow.setVisible(true);

    }

    // 操作命令方法
    public boolean setSwitchPosition(String switchId, Switch.SwitchPosition position) {
        boolean result = core.setSwitchPosition(switchId, position);
        if (result) {
            mainWindow.updateUI();
        }
        return result;
    }

    public boolean lockSwitch(String switchId) {
        boolean result = core.lockSwitch(switchId);
        if (result) {
            mainWindow.updateUI();
        }
        return result;
    }

    public boolean setSignalAspect(String signalId, Signal.SignalAspect aspect) {
        boolean result = core.setSignalAspect(signalId, aspect);
        if (result) {
            mainWindow.updateUI();
        }
        return result;
    }

    public Map<String,Route> getRoutes(){
        return core.getRoute();
    }

    public boolean setTrackCircuitOccupied(String trackId, boolean occupied) {
        boolean result = core.setTrackCircuitOccupied(trackId, occupied);
        if (result) {
            mainWindow.updateUI();
        }
        return result;
    }

    public boolean establishRoute(Route route) {
        boolean result = core.canEstablishRoute(route);
        if (result) {
            mainWindow.updateUI();
        }
        return result;
    }

    public boolean cancelRoute(String routeId) {
        boolean result = core.cancelRoute(routeId);
        if (result) {
            mainWindow.updateUI();
        }
        return result;
    }

    // 获取状态方法
    public Map<String, Switch> getSwitches() {
        return core.getSwitches();
    }

    public Map<String, Signal> getSignals() {
        return core.getSignals();
    }

    public Map<String, TrackCircuit> getTrackCircuits() {
        return core.getTrackCircuits();
    }

    public boolean check(Map<String,Signal> signals){
        return core.CheckP1andP2(signals);
    }


    public Map<String, Train> getTrainsController(){
        return core.getTrains();
    }

    public void updateTrains(){
        for(Train train:core.getTrains().values()){
            train.move();
        }
    }

    public void updateTrains2(){
        for(Train train:core.getTrains().values()){
            train.move2();
        }
    }
    public void updateTrains3(){
        for(Train train:core.getTrains().values()){
            train.move3();
        }
    }




}
